package cn.sofwin.blog.utils;

import java.io.File;
import java.net.URISyntaxException;

/**
 * 文件助手
 * @author ZX
 * @version 1.0
 */
public class FileHelper {

    /**
     * 获取当前应用的ClassPath根目录
     *
     * @author LetCode
     * @since 1.0
     * @param clazz
     * @return
     */
    public static String getRootClassPath(Class<?> clazz) {
        String rootClassPath = null;
        ClassLoader classLoader = clazz.getClassLoader();
        try {
            rootClassPath = classLoader.getResource("").toURI().getPath();
            rootClassPath = new File(rootClassPath).getAbsolutePath();
        } catch (URISyntaxException e) {
            rootClassPath = classLoader.getResource("").getPath();
            rootClassPath = new File(rootClassPath).getAbsolutePath();
        }

        return rootClassPath;
    }

    /**
     * 获取当前应用的WebContent根目录
     *
     * @author LetCode
     * @since 1.0
     * @param clazz
     * @return
     */
    public static String getWebRootPath(Class<?> clazz) {
        String webRootPath = null;
        try {
            webRootPath = clazz.getResource("/").toURI().getPath();
            webRootPath = new File(webRootPath).getParentFile().getParentFile().getCanonicalPath();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return webRootPath;
    }
}
